#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2021/8/2 19:45
# @Author  : 李中华
# @Email   : 1260429766@qq.com
# @File    : TestCase.py
import paramunittest
import unittest
from excel.ReadExcel import Excel
from common.headers import GetToken
from common.logger import Log
from common.readConfig import ReadConfig
import requests
from testcase.asserts.interfaceRequest import CheckResult
import json
from common.paramsplit import Paramsplit
from common.resultChange import ResultChange

loger = Log()
cls = Excel().get_xls()
get_paramchange = []

@paramunittest.parametrized(*cls)
class TestCase(unittest.TestCase):
    def setParameters(self, case_id, case_name, path_u, path_rl, method, datatype, param, code_suc, yes_no,
                      result, num2):
        self.case_id = str(case_id)
        self.case_name = str(case_name)
        self.path_u = str(path_u)
        self.path_rl = str(path_rl)
        self.method = str(method)
        self.datatype = str(datatype)
        self.param = str(param)
        self.code_suc = str(code_suc)
        self.yes_no = str(yes_no)
        self.result = str(result)
        self.num2 = str(num2)

    def setUp(self):
        try:
            self.session = requests.session()
            self.session,self.headers_token = GetToken().login_token(self.session)

        except Exception as e:
            loger.info(f"异常信息:{e}")
        loger.info(self.case_id + ':' + self.case_name)
        self.url = ReadConfig().get_http("serviceUrl") + self.path_rl
        print(type(self.param ))

    def testcase(self):

        if self.result == 'None' and self.num2 == 'None':
            CheckResult().checkResult(self.session,self.headers_token,self.url,self.datatype,self.method,self.code_suc,self.param)

        elif self.num2 != 'None':
            info = CheckResult().checkResult(self.session, self.headers_token, self.url, self.datatype, self.method,
                                      self.code_suc, self.param)
            infoDic = json.loads(info.text)
            if get_paramchange:
                get_paramchange.clear()
            else:
                get_paramchange.append(infoDic)
        else:
            final_param = Paramsplit().paramsplit(get_paramchange, self.result)  # 获取关联参数值，字典类型
            if '$' in self.param:
                self.param = ResultChange().resultchange(self.param,final_param) #替换关联参数值到参数列表中
            if '$' in self.path_rl:
                self.path_rl = ResultChange().resultchangeURL(self.path_rl,final_param)
            else:
                pass
                CheckResult().checkResult(self.session, self.headers_token, self.url, self.datatype, self.method,
                                      self.code_suc, self.param)

    def tearDown(self):
        loger.info("完结")

if __name__ == '__main__':
    # print(cls)
    unittest.main()